package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private TokenDecode tokenDecode;

    @GetMapping("/add")
    public Result add(@RequestParam("skuId") String skuId,@RequestParam("num") Integer num){
        /*String username = "itcast";*/
        String username = tokenDecode.getUserInfo().get("username");
        cartService.add(skuId,num,username);
        return new Result(true, StatusCode.OK,"加入购物车成功");
    }

    /**
     *  查询用户购物车
     *  @author TH
     *  @date 2019/12/2
     *  @return java.util.Map
    **/
    @GetMapping("/list")
    public Map list(){
        /*String username = "itcast";*/
        String username = tokenDecode.getUserInfo().get("username");
        return cartService.list(username);
    }
}
